var ConsultantDiagnosis = {//看诊详情  初始化
    id: "ConsultantDiagnosisTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

ConsultantDiagnosis.initColumn = function () {
    var columns = [
        {field: 'selectItem', radio: true},
        {title: 'id', field: 'id', visible: false, align: 'center', valign: 'middle'},
        {title: 'customerId', field: 'customerId', visible: false, align: 'center', valign: 'middle'},
        {title: '', field: 'customerSexHead ', visible: false, align: 'center', valign: 'middle'},
        {title: 'reservationsId', field: 'reservationsId', visible: false, align: 'center', valign: 'middle'},
        {
            title: '客户姓名', field: 'customerName', align: 'center', valign: 'middle', sortable: true, width: 120,
            formatter: function (value, row, index) {
                return "<a href='javascript:;' style='display: flex;align-items: center;' onclick = 'ConsultantDiagnosis.consultantDetails(" + '"' + row.customerId + '"' + ")' ><img src='" + row.customerSexHead + "' alt='' width='40px'><span class='portraitHeadName'>" + value + "</span></a>"
            }
        },
        {title: '手机号码', field: 'customerPhone', align: 'center', valign: 'middle', sortable: true, width: 140},
        {
            title: '性别', field: 'customerSex', align: 'center', valign: 'middle', sortable: true, width: 80,
            formatter: function (value, row, index) {
                return value === 1 ? '男' : value === 2 ? '女' : '未知';
            }
        },
        {title: '年龄', field: 'customerAge', align: 'center', valign: 'middle', sortable: true, width: 80},
        {title: '看诊门诊', field: 'outpatientName', align: 'center', valign: 'middle', sortable: true, width: 110},
        {title: '跟踪状态', field: 'followStatus', align: 'center', valign: 'middle', sortable: true, width: 110},
        {title: '消费状态', field: 'consumptionStatus', align: 'center', valign: 'middle', sortable: true, width: 110},
        {
            title: '开单状态', field: 'dealStatus', align: 'center', valign: 'middle', sortable: true, width: 110,
            formatter: function (value, row, index) {
                if (value === "已结账") {
                    return "<a href='javascript:;' onclick='ConsultantDiagnosis.opendealStatus(" + '"' + row.reservationsId + '"' + ")'>[已结账]</a>"
                } else {
                    return "未成交";
                }
            }
        },
        {title: '成交状态', field: 'isdealStatus', align: 'center', valign: 'middle', sortable: true, width: 120},
        {title: 'TC', field: 'fieldTc', align: 'center', valign: 'middle', sortable: true, width: 100},
        {title: '初诊时间', field: 'firstVisitDate', align: 'center', valign: 'middle', sortable: true, width: 120},
        {title: '看诊人', field: 'tcName', align: 'center', valign: 'middle', sortable: true, width: 100},
        {
            title: '看诊内容', field: 'diagnosisContent', align: 'center', valign: 'middle', sortable: true, width: 200,
            formatter: function (value, row, index) {
                var reg = new RegExp("& lt;br/& gt;", "g");
                var str = value.replace(reg, '<br>');
                return '<span title="' + str + '">' + str + '</span>'
            },
            cellStyle: function (value, row, index) {
                return {css: {"overflow": "hidden", "white-space": "nowrap", "text-overflow": "ellipsis"}}
            }
        },
        {title: '看诊时间', field: 'diagnosisTime', align: 'center', valign: 'middle', sortable: true, width: 200},
        {
            title: '操作', field: '', align: 'center', valign: 'middle', sortable: true, width: 80,
            formatter: function (value, row, index) {
                return '<button title="编辑" style="background: #ffffff;border: 1px solid #cccccc;padding: 1px 4px 0px 5px;" onclick="ConsultantDiagnosis.updateTcDiagnosis(\'' + row.id + '\')" type="button" class="btn"><span class="glyphicon glyphicon-edit"></span></button> ' +
                    '<button title="删除" onclick="ConsultantDiagnosis.deleteTcDiagnosis(\'' + row.id + '\',\'' + row.reservationsId + '\')" type="button" class="btn" style="background: #ffffff;border: 1px solid #cccccc;padding: 1px 4px 0px 5px;"><span class="glyphicon glyphicon-trash"></span></button>'
            }
        },
    ];
    return columns;
};

ConsultantDiagnosis.deleteTcDiagnosis = function (id, reservationsId) {//删除看诊
    if (confirm("确认删除本条看诊?")) {
        $.ajax({
            url: Feng.ctxPath + "/diagnosis/delete_tcDiagnosis",
            type: "post",
            data: {
                id: id,
                reservationsId: reservationsId
            },
            success: function (data) {
                Feng.success('操作成功!');
                window.ConsultantDiagnosis.search();
            },
            error: function (data) {
                alert('没有权限，请联系管理员!');
            }
        });
    }
};

ConsultantDiagnosis.updateTcDiagnosis = function (id) {//编辑看诊
    var index = layer.open({
        type: 2,
        title: '编辑看诊',
        area: ['40%', '50%'], //宽高
        fix: true, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/diagnosis/update_tcDiagnosis/' + id
    });
    this.layerIndex = index;
};

ConsultantDiagnosis.search = function () {//查询 回访详情列表
    ConsultantDiagnosis.table.refresh({query: ConsultantDiagnosis.formParams()});
};

ConsultantDiagnosis.resetSearch = function () {
    parent.$("#doctorTcInputPublic").val("");  //姓名   电话 微信 预约号
    $("#startDiagnosisTime").val(""); //开始时间
    $("#endDiagnosisTime").val("");  //结束时间
    $("#fieldTc").val("");   //tc
    $("#followStatus").val("");
    $("#dealStatus").val("");
    $("#consumptionStatus").val("");
    $("#outpatientName").val("");
    $("#tcName").val("");
    $(".timeBox > div button").removeClass('actionClick');
    ConsultantDiagnosis.search();
};

var parseParam = function (param) {//把对象拼接成URL的键值对参数param1=value1&param2=value2
    var paramStr = "";
    $.each(param, function (i) {
        paramStr += '&' + getSingleData(param, i);
    });
    return paramStr.substr(1);
};

var getSingleData = function (param, key) {
    return key + "=" + param[key];
};

ConsultantDiagnosis.inquire = function () {
    ConsultantDiagnosis.search();
};

ConsultantDiagnosis.consultantDetails = function (id) {//点击姓名进入详情
    var index = layer.open({
        type: 2,
        title: '预约客户详情',
        area: ['100%', '100%'], //宽高
        fix: true, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/consultantDetails/consultantToDetailByTab/' + id + '/4/2'
    });
    this.layerIndex = index;
};

ConsultantDiagnosis.opendealStatus = function (id) {
    $.ajax({
        url: Feng.ctxPath + "/reservations/getTotleMoneys",
        type: "post",
        data: {
            "id": id
        },
        success: function (data) {
            alert("成交金额：" + data + "元");
        },
        error: function (data) {
            alert('没有权限，请联系系统管理员!');
            parent.layer.close(parent.layer.getFrameIndex(window.name));
        }
    });
};

ConsultantDiagnosis.formParams = function () {//查询表单提交参数对象 @returns {{}}
    var queryData = {};
    queryData['sreach'] = parent.$("#doctorTcInputPublic").val().trim();  //姓名   电话   微信  预约号
    queryData['startDiagnosisTime'] = $("#startDiagnosisTime").val();
    queryData['endDiagnosisTime'] = $("#endDiagnosisTime").val();
    queryData['fieldTc'] = $("#fieldTc").val();
    queryData['tcName'] = $("#tcName").val();
    queryData['followStatus'] = $("#followStatus").val();
    queryData['dealStatus'] = $("#dealStatus").val();
    queryData['consumptionStatus'] = $("#consumptionStatus").val();
    queryData['outpatientName'] = $("#outpatientName",parent.document).val();
    queryData['abbreviation'] = $("#abbreviation", parent.document).val();
    return queryData;
};

ConsultantDiagnosis.today = function () {// 今日
    var date = new Date();
    var currentYear = date.getFullYear();       // 当前年份
    var currentMonth = date.getMonth() + 1;    // 当前月份
    var currentDay = date.getDate();         // 今日日期 几号
    if (currentMonth < 10) currentMonth = "0" + currentMonth;
    if (currentDay < 10) currentDay = "0" + currentDay;
    document.getElementById("startDiagnosisTime").value = currentYear + "-" + currentMonth + "-" + currentDay + " " + "00:00:00";
    document.getElementById("endDiagnosisTime").value = currentYear + "-" + currentMonth + "-" + currentDay + " " + "23:59:59";
    $(".timeBox > div button").eq(0).addClass('actionClick').siblings().removeClass('actionClick');
    ConsultantDiagnosis.search();
};

ConsultantDiagnosis.yesterday = function () {// 昨日
    var date = new Date();
    date.setDate(date.getDate() - 1);
    var currentYear = date.getFullYear();       // 当前年份
    var currentMonth = date.getMonth() + 1;    // 当前月份
    var currentDay = date.getDate();         // 今日日期 几号
    if (currentMonth < 10) currentMonth = "0" + currentMonth;
    if (currentDay < 10) currentDay = "0" + currentDay;
    document.getElementById("startDiagnosisTime").value = currentYear + "-" + currentMonth + "-" + currentDay + " " + "00:00:00";
    document.getElementById("endDiagnosisTime").value = currentYear + "-" + currentMonth + "-" + currentDay + " " + "23:59:59";
    $(".timeBox > div button").eq(1).addClass('actionClick').siblings().removeClass('actionClick')
    ConsultantDiagnosis.search();
};

ConsultantDiagnosis.thisWeek = function () {// 本周
    var date = new Date();
    var currentWeek = date.getDay();
    var stepSunDay = -currentWeek + 1;
    if (currentWeek == 0) {
        stepSunDay = -7;
    }
    var stepMonday = 7 - currentWeek;
    var time = date.getTime();
    var monday = new Date(time + stepSunDay * 24 * 3600 * 1000);
    var sunday = new Date(time + stepMonday * 24 * 3600 * 1000);
    var weekTimeStartYear = monday.getFullYear();       // 当前年份
    var weekTimeStartMonth = monday.getMonth() + 1;    // 当前月份
    var weekTimeStartDay = monday.getDate();         // 今日日期 几号
    if (weekTimeStartMonth < 10) weekTimeStartMonth = "0" + weekTimeStartMonth;
    if (weekTimeStartDay < 10) weekTimeStartDay = "0" + weekTimeStartDay;
    var weekTimeEndYear = sunday.getFullYear();       // 当前年份
    var weekTimeEndMonth = sunday.getMonth() + 1;    // 当前月份
    var weekTimeEndDay = sunday.getDate();         // 今日日期 几号
    if (weekTimeEndMonth < 10) weekTimeEndMonth = "0" + weekTimeEndMonth;
    if (weekTimeEndDay < 10) weekTimeEndDay = "0" + weekTimeEndDay;
    document.getElementById("startDiagnosisTime").value = weekTimeStartYear + "-" + weekTimeStartMonth + "-" + weekTimeStartDay + " " + "00:00:00";
    document.getElementById("endDiagnosisTime").value = weekTimeEndYear + "-" + weekTimeEndMonth + "-" + weekTimeEndDay + " " + "23:59:59";
    $(".timeBox > div button").eq(2).addClass('actionClick').siblings().removeClass('actionClick')
    ConsultantDiagnosis.search();
};

$("#doctorTcSearchAll", parent.document).click(function () {//主页搜索
    ConsultantDiagnosis.search();
});

function phonePrivileges(){   //隐藏手机号
    var phonePrivileges=parent.$("#phonePrivileges").val();
    if(phonePrivileges == 2){
        $('#ConsultantDiagnosisTable').bootstrapTable('hideColumn', 'customerPhone');
    }
}
$(function () {
    var defaultColunms = ConsultantDiagnosis.initColumn();
    var table = new BSTable(ConsultantDiagnosis.id, "/diagnosis/list", defaultColunms);
    table.setPaginationType("server");
    table.setQueryParams(ConsultantDiagnosis.formParams());
    ConsultantDiagnosis.table = table.init();
    ConsultantDiagnosis.today();
    checkDate = function () {ConsultantDiagnosis.search();};
    $(document).on('click', '#laydate_clear,#laydate_today,#laydate_ok', function () {ConsultantDiagnosis.search();});
    menuNameHideColumn($("#menuName").val(),'ConsultantDiagnosisTable');
    var seleteooutpatientName = $('#outpatientNameValue').val();
    if (seleteooutpatientName != "") {
        $("#outpatientName option[value='" + seleteooutpatientName + "']").attr("selected", "selected");
    }
    $("#zoom").click(function () {
        if ($("#zoom > span").hasClass('tabMark')) {
            $("#zoom > span").removeClass('tabMark').html('条件展开');
            $("#zoom > img,.divFlex").toggle();
            $(document).ready(function () {
                $('#ConsultantDiagnosisTable').bootstrapTable('resetView', {height: $(window).height() - 76});
            });
        } else {
            $("#zoom > span").addClass('tabMark').html('条件收起');
            $("#zoom > img,.divFlex").toggle();
            $(document).ready(function () {
                $('#ConsultantDiagnosisTable').bootstrapTable('resetView', {height: $(window).height() - 137});
            });
        }
    });
    phonePrivileges();
});